package com.zhss.redis.list;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;

import java.util.stream.IntStream;


/**
 * 做演示
 *
 * @author winston
 * @date 2021/09/23
 */
public class TodoDemo {
	static Jedis jedis = new Jedis(new HostAndPort("121.4.79.149", 6379));
	private static String list_key = "todo_event";
	private static int pageSize = 100;

	public static void add() {
		IntStream.rangeClosed(0, 10).forEach(i->{
			jedis.rpush(list_key, String.valueOf(i));
		});
	}

	public static void insert(ListPosition position, String target, String newTodo) {
		jedis.linsert(list_key, position, target, newTodo);
	}

	public static void delete(String target) {
		jedis.lrem(list_key,0, target);
	}

	public static void getPage(int page) {
		var startIndex = page * pageSize;
		var endIndex = ((page + 1) * pageSize-1);
		jedis.lrange(list_key, startIndex, endIndex).forEach(System.out::println);
	}

	public static void update() {
		jedis.lset(list_key, 1, "updatedValue");
	}

	public static void main(String[] args) {
//		add();
//		insert(ListPosition.AFTER, "3", "after3");
//		delete("after3");
		update();
		getPage(0);
	}
}
